<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body>

</body>
<script src="https://hoppinzq.com/static/js/vendor/jquery-3.5.1.min.js"></script>
<script src="https://unpkg.com/ajax-hook@3.0.3/dist/ajaxhook.min.js"></script>
<script>
    ah.proxy({
        //请求发起前进入
        onRequest: (config, handler) => {
            console.log(config.url)
            handler.next(config);
        },
        //请求发生错误时进入，比如超时；注意，不包括http状态码错误，如404仍然会认为请求成功
        onError: (err, handler) => {
            console.log(err.type)
            handler.next(err)
        },
        //请求成功后进入
        onResponse: (response, handler) => {
            console.log(response.response)
            handler.next(response)
        }
    })
    const { unProxy, originXhr } = ah.proxy({
        onRequest: (config, handler) => {
            if (config.url === 'https://aa/') {
                handler.resolve({
                    config: config,
                    status: 200,
                    headers: {'content-type': 'text/text'},
                    response: 'hi world'
                })
            } else {
                handler.next(config);
            }
        },
        onError: (err, handler) => {
            if (err.config.url === 'https://bb/') {
                handler.resolve({
                    config: err.config,
                    status: 200,
                    headers: {'content-type': 'text/text'},
                    response: 'hi world'
                })
            } else {
                handler.next(err)
            }
        },
        onResponse: (response, handler) => {
            if (response.config.url === location.href) {
                handler.reject({
                    config: response.config,
                    type: 'error'
                })
            } else {
                handler.next(response)
            }
        }
    })

    // 使用jQuery发起网络请求
    function testJquery(url) {
        $.get(url).done(function (d) {
            console.log(d)
        }).fail(function (e) {
            console.log('hi world')
        })
    }

    //测试
    testJquery('https://aa/');
    testJquery('https://bb/');
    testJquery(location.href)

    // 取消拦截
    unProxy();
</script>
</html>